<?php

namespace app\controller;

use think\Request;
use app\service\ImportExportService;
use Exception;

class Import
{
    protected $importExportService;

    public function __construct()
    {
        $this->importExportService = new ImportExportService();
    }

    /**
     * 导出为JSON
     */
    public function exportJson(Request $request)
    {
        try {
            $userId = $request->userId;
            $categoryId = $request->param('category_id');

            $data = $this->importExportService->exportToJson($userId, $categoryId);

            return success($data, '导出成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 导出为CSV
     */
    public function exportCsv(Request $request)
    {
        try {
            $userId = $request->userId;
            $categoryId = $request->param('category_id');

            $data = $this->importExportService->exportToCsv($userId, $categoryId);

            return success($data, '导出成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 导入JSON数据
     */
    public function importJson(Request $request)
    {
        try {
            $userId = $request->userId;
            $jsonData = $request->param('data');

            if (is_string($jsonData)) {
                $jsonData = json_decode($jsonData, true);
            }

            if (!$jsonData) {
                return error('数据格式错误');
            }

            $options = [
                'skip_duplicates' => $request->param('skip_duplicates', true),
                'update_existing' => $request->param('update_existing', false)
            ];

            $result = $this->importExportService->importFromJson($userId, $jsonData, $options);

            return success($result, '导入完成');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 导入CSV数据
     */
    public function importCsv(Request $request)
    {
        try {
            $userId = $request->userId;
            $csvData = $request->param('data');

            if (!is_array($csvData)) {
                return error('CSV数据格式错误');
            }

            $options = [
                'skip_duplicates' => $request->param('skip_duplicates', true)
            ];

            $result = $this->importExportService->importFromCsv($userId, $csvData, $options);

            return success($result, '导入完成');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 创建备份
     */
    public function createBackup(Request $request)
    {
        try {
            $userId = $request->userId;

            $result = $this->importExportService->createBackup($userId);

            return success($result, '备份创建成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 列出备份
     */
    public function listBackups(Request $request)
    {
        try {
            $userId = $request->userId;

            $backups = $this->importExportService->listBackups($userId);

            return success($backups, '获取成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 恢复备份
     */
    public function restoreBackup(Request $request)
    {
        try {
            $userId = $request->userId;
            $filename = $request->param('filename');

            if (empty($filename)) {
                return error('备份文件名不能为空');
            }

            $result = $this->importExportService->restoreBackup($userId, $filename);

            return success($result, '备份恢复成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }

    /**
     * 删除备份
     */
    public function deleteBackup(Request $request)
    {
        try {
            $userId = $request->userId;
            $filename = $request->param('filename');

            if (empty($filename)) {
                return error('备份文件名不能为空');
            }

            $this->importExportService->deleteBackup($userId, $filename);

            return success(null, '删除成功');

        } catch (Exception $e) {
            return error($e->getMessage());
        }
    }
}
